package viqiang.ye.mallpuny.service.impl;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import viqiang.ye.mallpuny.nosql.elasticsearch.document.mongodb.document.MemberReadHistory;
import viqiang.ye.mallpuny.nosql.elasticsearch.document.mongodb.document.repository.MemberReadHistoryRepository;
import viqiang.ye.mallpuny.service.MemberReadHistoryService;

import java.util.Date;
import java.util.List;

@Service
public class MemberReadHistoryServiceImpl implements MemberReadHistoryService {

    @Autowired
    private MemberReadHistoryRepository memberReadHistoryRepository;

    @Override
    public int create(MemberReadHistory memberReadHistory) {
        memberReadHistory.setId(null);
        memberReadHistory.setCreateTime(new Date());
        memberReadHistoryRepository.save(memberReadHistory);
        return 1;
    }

    @Override
    public int delete(List<String> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            List<MemberReadHistory> memberReadHistories = Lists.newArrayList();
            ids.stream().forEach(i -> {
                MemberReadHistory vo = new MemberReadHistory();
                vo.setId(i);
                memberReadHistories.add(vo);
            });
            memberReadHistoryRepository.deleteAll(memberReadHistories);
            return ids.size();
        }
        return 0;
    }

    @Override
    public List<MemberReadHistory> list(Long memberId) {
        return memberReadHistoryRepository.findByMemberIdOrderByCreateTimeDesc(memberId);
    }
}
